﻿@model NewsLetterSubscriptionListModel

@{
    var defaultGridPageSize = EngineContext.Current.Resolve<SDF.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<SDF.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.Promotions.newsLetterSubscriptions").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Newsletter subscriptions");
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="content-header clearfix">
        <h1 class="pull-left">
            @T("Admin.Promotions.NewsLetterSubscriptions")
        </h1>
        <div class="pull-right">
            <button type="submit" name="exportcsv" class="btn bg-green">
                <i class="fa fa-download"></i>
                @T("Admin.Common.ExportToCsv")
            </button>
            <button type="button" name="importcsv" class="btn bg-olive" data-toggle="modal" data-target="#importcsv-window">
                <i class="fa fa-upload"></i>
                @T("Admin.Common.ImportFromCsv")
            </button>
        </div>
    </div>

    <div class="content">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default panel-search">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.SDFLabelFor(model => model.StartDate)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.SDFEditorFor(model => model.StartDate)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.SDFLabelFor(model => model.EndDate)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.SDFEditorFor(model => model.EndDate)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.SDFLabelFor(model => model.SearchEmail)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.SDFEditorFor(model => model.SearchEmail)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.SDFLabelFor(model => model.ActiveId)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.SDFDropDownList("ActiveId", Model.ActiveList)
                                    </div>
                                </div>
                                <div class="form-group" @(Model.AvailableProjects.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                    <div class="col-md-4">
                                        @Html.SDFLabelFor(model => model.ProjectId)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.SDFDropDownList("ProjectId", Model.AvailableProjects)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.SDFLabelFor(model => model.CustomerRoleId)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.SDFDropDownList("CustomerRoleId", Model.AvailableCustomerRoles)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-8 col-md-offset-4">
                                        <button type="button" id="search-subscriptions" class="btn btn-primary btn-search">
                                            <i class="fa fa-search"></i>
                                            @T("Admin.Common.Search")
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-body">
                        <div id="newsletter-subscriptions-grid"></div>

                        <script>
                            $(document).ready(function() {
                                $("#newsletter-subscriptions-grid").kendoGrid({
                                    dataSource: {
                                        type: "json",
                                        transport: {
                                            read: {
                                                url: "@Html.Raw(Url.Action("SubscriptionList", "NewsLetterSubscription"))",
                                                type: "POST",
                                                dataType: "json",
                                                data: additionalData
                                            },
                                            update: {
                                                url: "@Html.Raw(Url.Action("SubscriptionUpdate", "NewsLetterSubscription"))",
                                                type: "POST",
                                                dataType: "json",
                                                data: addAntiForgeryToken
                                            },
                                            destroy: {
                                                url: "@Html.Raw(Url.Action("SubscriptionDelete", "NewsLetterSubscription"))",
                                                type: "POST",
                                                dataType: "json",
                                                data: addAntiForgeryToken
                                            }
                                        },
                                        schema: {
                                            data: "Data",
                                            total: "Total",
                                            errors: "Errors",
                                            model: {
                                                id: "Id",
                                                fields: {
                                                    Email: { editable: true, type: "string" },
                                                    Active: { editable: true, type: "boolean" },
                                                    ProjectName: { editable: false, type: "string" },
                                                    CreatedOn: { editable: false, type: "date" }
                                                }
                                            }
                                        },
                                        requestEnd: function(e) {
                                            if (e.type == "update") {
                                                this.read();
                                            }
                                        },
                                        error: function(e) {
                                            display_kendoui_grid_error(e);
                                            // Cancel the changes
                                            this.cancelChanges();
                                        },
                                        pageSize: @(defaultGridPageSize),
                                        serverPaging: true,
                                        serverFiltering: true,
                                        serverSorting: true
                                    },
                                    pageable: {
                                        refresh: true,
                                        pageSizes: [@(gridPageSizes)]
                                    },
                                    editable: {
                                        confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                        mode: "inline"
                                    },
                                    scrollable: false,
                                    columns: [
                                        {
                                            field: "Email",
                                            title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Email")",
                                            width: 300
                                        }, {
                                            field: "Active",
                                            title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Active")",
                                            width: 100,
                                            headerAttributes: { style: "text-align:center" },
                                            attributes: { style: "text-align:center" },
                                            template: '# if(Active) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                                        }, {
                                            field: "ProjectName",
                                            title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Project")",
                                            width: 300
                                        }, {
                                            field: "CreatedOn",
                                            title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.CreatedOn")",
                                            width: 200,
                                            type: "date",
                                            format: "{0:G}"
                                        }, {
                                            command: [
                                                {
                                                    name: "edit",
                                                    text: {
                                                        edit: "@T("Admin.Common.Edit")",
                                                        update: "@T("Admin.Common.Update")",
                                                        cancel: "@T("Admin.Common.Cancel")"
                                                    }
                                                }, {
                                                    name: "destroy",
                                                    text: "@T("Admin.Common.Delete")"
                                                }
                                            ],
                                            width: 200
                                        }
                                    ]
                                });
                            });
                        </script>

                        <script type="text/javascript">
                            $(document).ready(function() {
                                $('#search-subscriptions').click(function() {
                                    var grid = $('#newsletter-subscriptions-grid').data('kendoGrid');
                                    grid.dataSource.page(1); //new search. Set page size to 1
                                    //grid.dataSource.read(); we already loaded the grid above using "page" function
                                    return false;
                                });

                                $("#@Html.FieldIdFor(model => model.SearchEmail)").keydown(function(event) {
                                    if (event.keyCode == 13) {
                                        $("#search-subscriptions").click();
                                        return false;
                                    }
                                });
                            });

                            function additionalData() {
                                var data = {
                                    StartDate: $('#@Html.FieldIdFor(model => model.StartDate)').val(),
                                    EndDate: $('#@Html.FieldIdFor(model => model.EndDate)').val(),
                                    ActiveId: $('#@Html.FieldIdFor(model => model.ActiveId)').val(),
                                    ProjectId: $('#@Html.FieldIdFor(model => model.ProjectId)').val(),
                                    CustomerRoleId: $('#@Html.FieldIdFor(model => model.CustomerRoleId)').val(),
                                    SearchEmail: $('#@Html.FieldIdFor(model => model.SearchEmail)').val()
                                };
                                addAntiForgeryToken(data);
                                return data;
                            }
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
}

@*import emails form*@
<div id="importcsv-window" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="importcsv-window-title">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="importcsv-window-title">@T("Admin.Common.ImportFromCsv")</h4>
            </div>
            @using (Html.BeginForm("ImportCsv", "NewsLetterSubscription", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <div class="form-horizontal">
                    <div class="modal-body">
                        @Html.AntiForgeryToken()
                        <div class="form-group">
                            <div class="col-md-2">
                                <div class="label-wrapper">
                                    <label class="control-label">
                                        @T("Admin.Common.CsvFile")
                                    </label>
                                </div>
                            </div>
                            <div class="col-md-10">
                                <input type="file" id="importcsvfile" name="importcsvfile" class="form-control" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">
                            @T("Admin.Common.ImportFromCsv")
                        </button>
                    </div>
                </div>
            }
        </div>
    </div>
</div>